#include "ImageShader.h"
#include "IHudElement.h"


ImageShader::ImageShader(void)
{
}

ImageShader::~ImageShader(void)
{
}

HRESULT ImageShader::Init(const char* effectName,const char* VsName, const char* vsVersion, const char* PsName,const char* psVersion,VertexLayout vl,const char* GsName,const char* GsVersion)
{
	HRESULT hr = S_OK;
	hr = IShader::Init(effectName,VsName,vsVersion,PsName,psVersion,vl);
	CheckError(hr);

	// Create the constant buffers
	D3D11_BUFFER_DESC bd;
    ZeroMemory( &bd, sizeof(bd) );
    bd.Usage = D3D11_USAGE_DEFAULT;                    
    bd.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
    bd.CPUAccessFlags = 0;    
    bd.ByteWidth = sizeof(OnRezise);	
	hr = EngineStuff::EGraphicsDevice->CreateBuffer( &bd, NULL, &buffer);
    CheckError(hr);
	
    // Create the sample state
    D3D11_SAMPLER_DESC sampDesc;
    ZeroMemory( &sampDesc, sizeof(sampDesc) );
    sampDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
    sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
    sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
    sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
    sampDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
    sampDesc.MinLOD = 0;
    sampDesc.MaxLOD = D3D11_FLOAT32_MAX;
	hr = EngineStuff::EGraphicsDevice->CreateSamplerState( &sampDesc, &g_pSamplerLinear );
    CheckError(hr);

	return hr;
}
void ImageShader::SetShaderOnDevice(RenderableObject obj)
{		
    EngineStuff::EImediateDeviceContext->UpdateSubresource( buffer, 0, NULL, &OnRezise, 0, 0 );	    
	EngineStuff::EImediateDeviceContext->VSSetConstantBuffers( 0, 1, &buffer);    

	ID3D11ShaderResourceView* texture = obj.IHUDELEMENT->GetImage2D()->GetTexture();
	EngineStuff::EImediateDeviceContext->PSSetShaderResources(0, 1, &texture );
    EngineStuff::EImediateDeviceContext->PSSetSamplers( 0, 1, &g_pSamplerLinear );

	IShader::SetShaderOnDevice(obj);
}

void ImageShader::Update(RenderableObject obj,Camera* camera)
{
	XMStoreFloat4x4(&OnRezise.Projection , XMMatrixTranspose(obj.IHUDELEMENT->GetImage2D()->GetProjectionMatrix()));
}